# HydroPulse-DX PostgreSQL 15 + TimescaleDB Docker镜像
# 用于教学演示的水文数据管理系统数据库环境

FROM postgres:15

# 设置环境变量
ENV POSTGRES_DB=hydropulse
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=water
ENV TIMESCALEDB_TELEMETRY=off

# 安装必要的依赖
RUN apt-get update && apt-get install -y \
    wget \
    gnupg \
    lsb-release \
    && rm -rf /var/lib/apt/lists/*

# 添加TimescaleDB APT仓库
RUN echo "deb https://packagecloud.io/timescale/timescaledb/debian/ $(lsb_release -c -s) main" > /etc/apt/sources.list.d/timescaledb.list
RUN wget --quiet -O - https://packagecloud.io/timescale/timescaledb/gpgkey | apt-key add -

# 安装TimescaleDB
RUN apt-get update && apt-get install -y \
    timescaledb-2-postgresql-15 \
    && rm -rf /var/lib/apt/lists/*

# 配置PostgreSQL
RUN echo "shared_preload_libraries = 'timescaledb'" >> /usr/share/postgresql/postgresql.conf.sample
RUN echo "max_connections = 200" >> /usr/share/postgresql/postgresql.conf.sample
RUN echo "shared_buffers = 256MB" >> /usr/share/postgresql/postgresql.conf.sample
RUN echo "effective_cache_size = 1GB" >> /usr/share/postgresql/postgresql.conf.sample
RUN echo "work_mem = 4MB" >> /usr/share/postgresql/postgresql.conf.sample
RUN echo "maintenance_work_mem = 64MB" >> /usr/share/postgresql/postgresql.conf.sample

# 创建初始化脚本目录
RUN mkdir -p /docker-entrypoint-initdb.d

# 复制初始化脚本
COPY docker-init.sql /docker-entrypoint-initdb.d/

# 暴露PostgreSQL端口
EXPOSE 5432

# 设置数据目录
VOLUME ["/var/lib/postgresql/data"]

# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD pg_isready -U $POSTGRES_USER -d $POSTGRES_DB

# 启动说明
LABEL description="PostgreSQL 15 + TimescaleDB for HydroPulse-DX教学演示"
LABEL version="1.0"
LABEL maintainer="HydroPulse-DX Team"